package com.finger.hospservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.finger.hospservice.service.HospSetService;
import com.finger.modelservice.model.hosp.HospitalSet;
import com.finger.modelservice.result.Result;
import com.finger.modelservice.vo.hosp.HospitalSetQueryVo;
import com.finger.serviceutil.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/admin/hosp/hospitalset")
public class HospSetController {

    @Autowired
    private HospSetService hospSetService;

    // 1 查找全部医院设置
    @GetMapping("/findAllHospitalSet")
    public Result findAllHospitalSet(){

        List<HospitalSet> list = hospSetService.list(null);

        return Result.ok(list);
    }

    // 2 根据id删除医院设置
    @DeleteMapping("{id}")
    public Result removeHospitalSetById(@PathVariable String id){

        hospSetService.removeById(id);

        return Result.ok();
    }

    // 3 添加医院设置
    @PostMapping("/addHospitalSet")
    public Result addHospitalSet(@RequestBody HospitalSet hospitalSet){

        hospitalSet.setStatus(1);

        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+new Random().nextInt(1000)));

        hospSetService.save(hospitalSet);

        return Result.ok();
    }

    // 4 更新医院设置
    @PutMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){

        boolean success = hospSetService.updateById(hospitalSet);

        if(success){

            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    // 5 根据条件分页查询医院设置
    @PostMapping("/getPageHospitalSet/{page}/{size}")
    public Result getPageHospitalSet(
            @PathVariable long page,
            @PathVariable long size,
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){

        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();

        Page<HospitalSet> hospitalSetPage = new Page<>(page,size);

        QueryWrapper<HospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();

        hospitalSetQueryWrapper.orderByDesc("gmt_create");

        if(!StringUtils.isEmpty(hoscode)){

            hospitalSetQueryWrapper.eq("hoscode",hoscode);
        }

        if(!StringUtils.isEmpty(hosname)){
            hospitalSetQueryWrapper.like("hosname",hosname);
        }

        IPage<HospitalSet> pages = hospSetService.page(hospitalSetPage, hospitalSetQueryWrapper);

        return Result.ok(pages);
    }

    // 6 根据id查找医院设置
    @GetMapping("/{id}")
    public Result getHospitalSetById(@PathVariable String id){

        HospitalSet hospitalSet = hospSetService.getById(id);

        return Result.ok(hospitalSet);
    }

    // 7批量删除医院设置
    @DeleteMapping("/removeBatch")
    public Result removeBatch(@RequestBody List<String> ids){

        boolean b = hospSetService.removeByIds(ids);

        if(b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    // 8 修改医院设置状态
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable String id,
                               @PathVariable Integer status){

        HospitalSet hospital = hospSetService.getById(id);

        hospital.setStatus(status);

        boolean b = hospSetService.updateById(hospital);

        if(b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    // 9
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable String id){

        HospitalSet hospitalSet = hospSetService.getById(id);

        String hoscode = hospitalSet.getHoscode();
        hospitalSet.getSignKey();

        return Result.ok();
    }

}
